package com.czl.module_storehouse.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.czl.module_base.utils.NumberUtils;
import com.czl.module_storehouse.R;

/**
 * @author by xubin,
 * @date on 2022/2/21.
 * PS: Not easy to write code, please indicate.
 */
public class CommonCounterView extends FrameLayout implements View.OnTouchListener, View.OnFocusChangeListener, View.OnClickListener, TextWatcher {
    public static final int MAX_COUNT = Integer.MAX_VALUE;
    /**
     * 文本输入
     */
    public EditText mEtQuantity;
    /**
     * 文本数量
     */
    private int count = 0;
    /**
     * 最大数量
     */
    private int maxCount = 0;
    /**
     * 最小数量
     */
    private int minCount = 0;
    /**
     * 触摸tag
     */
    protected int mTouchItemPosition = -1;

    private OnTextChangeListener mOnTextChangeListener;

    public CommonCounterView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public CommonCounterView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CommonCounterView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.layout_common_select_quantity, this);
        mEtQuantity = findViewById(R.id.tv_quantity);
        findViewById(R.id.text_remove).setOnClickListener(this);
        findViewById(R.id.text_add).setOnClickListener(this);
    }

    public void setTextChangeListener(OnTextChangeListener textChangeListener) {
        mOnTextChangeListener = textChangeListener;
    }

    public int getTouchItemPosition() {
        return mTouchItemPosition;
    }

    public void setTouchItemPosition(int touchItemPosition) {
        mTouchItemPosition = touchItemPosition;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        setEditTextTag(String.valueOf(count));
    }

    public int getMinCount() {
        return minCount;
    }

    public void setMinCount(int minCount) {
        this.minCount = minCount;
    }

    protected int getMaxCount() {
        return this.maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public void setEditTextTag(String text) {
        if (mEtQuantity.getTag() != null && mEtQuantity.getTag() instanceof TextWatcher) {
            mEtQuantity.removeTextChangedListener((TextWatcher) mEtQuantity.getTag());
        }
        mEtQuantity.setText(text);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int count;
                int minCount = getMinCount();
                if (NumberUtils.isNumber(editable.toString())) {
                    count = Integer.parseInt(editable.toString());
                    if (count < minCount) {
                        count = minCount;
                        mEtQuantity.setText(String.valueOf(count));
                        return;
                    } else if (count > maxCount) {
                        count = maxCount;
                        mEtQuantity.setText(String.valueOf(count));
                        return;
                    }
                } else {
                    count = 0;
                }
                mEtQuantity.setSelection(mEtQuantity.getText().length());
                if (mOnTextChangeListener != null) {
                    mOnTextChangeListener.onTextChange(CommonCounterView.this, count);
                }
            }
        };
        mEtQuantity.addTextChangedListener(textWatcher);
        mEtQuantity.setTag(textWatcher);
    }

    @SuppressLint("ClickableViewAccessibility")
    public void addEditTextTag(int tag) {
        mEtQuantity.setOnTouchListener(this);
        mEtQuantity.setOnFocusChangeListener(this);
        mEtQuantity.setTag(tag);
        //保证每个时刻只有一个EditText能获取到焦点
        if (mTouchItemPosition != -1 && tag == mTouchItemPosition) {
            mEtQuantity.requestFocus();
        } else {
            mEtQuantity.clearFocus();
        }
    }

    public void removeEditTextTag() {
        if (mEtQuantity.getTag() != null && mEtQuantity.getTag() instanceof TextWatcher) {
            mEtQuantity.removeTextChangedListener((TextWatcher) mEtQuantity.getTag());
        }
    }

    public void addEditTextTag(TextWatcher textWatcher) {
        mEtQuantity.setTag(textWatcher);
    }

    public void addTextChangedListener(TextWatcher textWatcher) {
        mEtQuantity.addTextChangedListener(textWatcher);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText editText = (EditText) v;
        if (hasFocus) {
            editText.addTextChangedListener(this);
        } else {
            editText.removeTextChangedListener(this);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        //onTouch 获取焦点
        if (event.getAction() == MotionEvent.ACTION_UP) {
            EditText editText = (EditText) view;
            mTouchItemPosition = (int) editText.getTag();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (R.id.text_remove == v.getId()) {
            //减
            if (count > minCount) {
                count--;
                mEtQuantity.setText(String.valueOf(count));
            }
        } else if (R.id.text_add == v.getId()) {
            //加
            if (count < getMaxCount()) {
                count++;
                mEtQuantity.setText(String.valueOf(count));
            }
        }
        mEtQuantity.clearFocus();
        /*if (mOnTextChangeListener != null) {
            mOnTextChangeListener.onTextChange(this, count);
        }*/
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (NumberUtils.isNumber(s.toString())) {
            int number = Integer.parseInt(s.toString());
            if (number > getMaxCount()) {
                count = getMaxCount();
                mEtQuantity.setText(String.valueOf(count));
                return;
            } else if (number < minCount) {
                count = minCount;
                mEtQuantity.setText(String.valueOf(count));
                return;
            } else {
                count = number;
            }
        }
        mEtQuantity.setSelection(mEtQuantity.getText().length());
        if (mOnTextChangeListener != null) {
            mOnTextChangeListener.onTextChange(this, count);
        }
    }

    public interface OnTextChangeListener {

        void onTextChange(CommonCounterView view, int count);
    }
}
